﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using LinqObj;
using System.Linq;

/// <summary>
/// Summary description for OrderListBuilder
/// </summary>

namespace ToolBoxPro
{
    public class OrderListBuilder
    {
        private List<Order> orderList;
        private IDictionary<int, Service_Category> categoryLookup;
        private Property property;
        public Control divOrderList;
        public const string PRINT_HINT = "print_hint";
        public const string REDIRECT_BUTTON = "bt_GetProposeForm";
        public const string SUBMIT_BUTTON = "bt_Submit";
        private Control form;
        private ToolboxDataContext td = new ToolboxDataContext();

        public Control BuildOrderList()
        {
            return form;
        }

        //public OrderListBuilder(IEnumerable<Order> paraOrderList, Property _property, IDictionary<int, Service_Category> categoryLookup, int popupamt)
        public OrderListBuilder(IEnumerable<Order> paraOrderList, Property _property, IDictionary<int, Service_Category> categoryLookup)
        {
            if (paraOrderList != null)
                this.orderList = (List<Order>)paraOrderList;
            else
                this.orderList = new List<Order>();
            this.property = _property;
            this.categoryLookup = categoryLookup;

            form = new Control();
            //HtmlGenericControl form2 = new HtmlGenericControl("form");
            //form2.Attributes["action"] = "print.aspx";
            //form2.Attributes["id"] = "form2";
            //form2.Attributes["name"] = "form2";
            //form2.Attributes["method"] = "post";

            

            HtmlGenericControl divSubmitBt = new HtmlGenericControl("div");
            HtmlGenericControl hiddenInnerHtml = new HtmlGenericControl("input");
            hiddenInnerHtml.Attributes["type"] = "hidden";
            hiddenInnerHtml.Attributes["id"] = "task_num";
            hiddenInnerHtml.Attributes["name"] = "task_num";
            hiddenInnerHtml.Attributes["value"] = this.property.property_id.ToString();

            //("<input type='hidden' id='task_num' name='task_num' value='{0}'/>", this.property.property_id);

            //hiddenInnerHtml = "<input type='submit' value='Submit' name='submitShopptingList' id='submitShoppingList' />" + hiddenInnerHtml;

            //Button submit = new Button();
            //submit.ID = OrderListBuilder.SUBMIT_BUTTON;
            //submit.Text = "Submit";
            //divSubmitBt.Controls.Add(submit);

            //Button postBackBt = new Button();
            //postBackBt.ID = OrderListBuilder.REDIRECT_BUTTON;
            //divSubmitBt.Controls.Add(postBackBt);

            //if (property._AllocatedStatus.Equals(Property.ALLOCATE_ORDERD))
            //{
            //    submit.Enabled = false;
            //    postBackBt.Enabled = false;
            //}

            form.Controls.Add(divSubmitBt);

            decimal totalPrice = 0;
            decimal otherprice = 0;
            HtmlGenericControl divListArea = new HtmlGenericControl("div");
            divListArea.Attributes["id"] = "ListArea";
            form.Controls.Add(divListArea);

            HtmlGenericControl divOrdersForPint = new HtmlGenericControl("div");
            divOrdersForPint.Attributes["id"] = "divOrdersForPint";            
            divOrdersForPint.InnerHtml = "<table width='100%'>";


            
            foreach(int val in categoryLookup.Keys){
                Service_Category category = categoryLookup[val];

                //Service_Category category = categoryLookup[key];

                System.Text.StringBuilder sb = new System.Text.StringBuilder();

                if (category.service_category1.Trim().IndexOf(' ') != -1)
                {
                    string[] str = category.service_category1.Trim().Split(' ');
                    for (int i = 0; i < str.Length; i++)
                    {
                        sb.Append(str[i]);
                    }
                }
                else
                {
                    sb.Append(category.service_category1.Trim());
                }                

                HtmlGenericControl divCategory = new HtmlGenericControl("div");
                divCategory.Attributes["id"] = "div_" + sb.ToString();
                divListArea.Controls.Add(divCategory);

                HtmlGenericControl h3CategoryName = new HtmlGenericControl("div");                
                h3CategoryName.Attributes["style"] = "background:#555555; font-size:11px; font-weight:bold; padding:10px 8px 8px 10px; border-bottom:1px solid #191a1b; text-align:left; vertical-align:middle;";
                h3CategoryName.InnerText = category.service_category1;
                divCategory.Controls.Add(h3CategoryName);

                List<Order> matchedOrders = orderList.FindAll(matchCategoryName(category));
                int j = 0;
                if (matchedOrders.Count > 0)
                {
                    divOrdersForPint.InnerHtml += "<tr><td colspan='2'><div style='background:#555555; font-size:11px; font-weight:bold; padding:10px 8px 8px 10px; border-bottom:1px solid #191a1b; text-align:left; vertical-align:middle;'>" + category.service_category1 + "</div></tr></td>";
                }
                foreach (Order order in matchedOrders)
                {                                        
                    HtmlGenericControl divOrderEntry = new HtmlGenericControl("div");
                    divOrderEntry.Attributes["id"] = order.service_id.ToString();
                    divCategory.Controls.Add(divOrderEntry);

                    HtmlTable table1 = new HtmlTable();
                    table1.Attributes["width"] = "100%";
                    table1.Attributes["border"] = "0";
                    table1.Attributes["align"] = "center";
                    table1.Attributes["cellpadding"] = "0";
                    table1.Attributes["cellspacing"] = "0";

                    HtmlTableRow tr1 = new HtmlTableRow();


                    /* 1 */
                    
                    HtmlTableCell tc1 = new HtmlTableCell();
                    tc1.Attributes["class"] = "btmbrdp";
                    tc1.Attributes["width"] = "140";
                    tc1.InnerText = order.Service.service_name;

                    HtmlGenericControl input_ServiceName = new HtmlGenericControl("input");
                    input_ServiceName.Attributes["type"] = "hidden";
                    input_ServiceName.Attributes["value"] = order.Service.service_name;
                    input_ServiceName.Attributes["name"] = "service_" + order.service_id;
                    tc1.Controls.Add(input_ServiceName);
                    tr1.Controls.Add(tc1);


                    /* 2 */

                    HtmlTableCell tc2 = new HtmlTableCell();
                    tc2.Attributes["class"] = "btmbrdp1";
                    tc2.Attributes["width"] = "40";
                    
                    if(order.Service.service_charge == 0)
                        tc2.InnerHtml = "<div class='PriceValue' id='" + order.Service.service_charge.ToString("C") + "'>Quote</div>";
                    else
                        tc2.InnerHtml = "<div class='PriceValue' id='" + order.Service.service_charge.ToString("C") + "'>" + order.Service.service_charge.ToString("C") + "</div>";
                    
                    tr1.Controls.Add(tc2);

                    divOrdersForPint.InnerHtml += "<tr>";
                    divOrdersForPint.InnerHtml += "<td class='btmbrdp' width='140'>" + order.Service.service_name + "</td>";

                    if (order.Service.service_charge == 0)
                        divOrdersForPint.InnerHtml += "<td class='btmbrdp1' width='40'><div class='PriceValue' id='" + order.Service.service_charge.ToString("C") + "'>Quote</div></td>";
                    else
                        divOrdersForPint.InnerHtml += "<td class='btmbrdp1' width='40'><div class='PriceValue' id='" + order.Service.service_charge.ToString("C") + "'>" + order.Service.service_charge.ToString("C") + "</div></td>";

                    divOrdersForPint.InnerHtml += "</tr>";
                    /* 3 */ 

                    //HtmlTableCell tc3 = new HtmlTableCell();
                    //tc3.Attributes["class"] = "btmbrdp1";
                    //tc3.Attributes["width"] = "90";
                    //tc3.Attributes["align"] = "left";                    

                    //HtmlGenericControl input_ServiceDueDate = new HtmlGenericControl("input");
                    //input_ServiceDueDate.Attributes["id"] = "DUE_DATE";
                    //input_ServiceDueDate.Attributes["style"] = "width:70px;";
                    //input_ServiceDueDate.Attributes["type"] = "text";
                    
                    //String _dueDate = FormContract.GetDateLabel(order.due_date);
                    //if (_dueDate == "")
                    //    _dueDate = FormContract.GetDateLabel(property.schedule_date);
                    //input_ServiceDueDate.Attributes["value"] = _dueDate;

                    //input_ServiceDueDate.Attributes["name"] = Order.DUE_PREFIX + order.service_id;
                    
                    //tc3.Controls.AddAt(0,input_ServiceDueDate);

                    //HtmlImage img = new HtmlImage();
                    //img.Attributes["title"] = "calender";
                    //img.Attributes["alt"] = "calender";
                    //img.Attributes["src"] = "images/calender.gif";
                    //tc3.Controls.AddAt(1, img);
                    //tr1.Controls.Add(tc3);


                    /* 4 */

                    HtmlTableCell tc4 = new HtmlTableCell();
                    tc4.Attributes["class"] = "btmbrdp1";
                    tc4.Attributes["width"] = "40";
                    tc4.Attributes["align"] = "center";                    

                    if (category.service_category1.Trim().ToLower() == "entry package"
                        || category.service_category1.Trim().ToLower() == "standard package"
                        || category.service_category1.Trim().ToLower() == "premium package"
                        || category.service_category1.Trim().ToLower() == "premium"
                        || category.service_category1.Trim().ToLower() == "intro package redhot deal"
                        )
                    {
                        if ((matchedOrders.Count-1) == j)
                        {
                            HtmlGenericControl input_ServiceCharge = new HtmlGenericControl("input");
                            input_ServiceCharge.Attributes["class"] = "inputSibling";
                            input_ServiceCharge.Attributes["type"] = "hidden";
                            input_ServiceCharge.Attributes["value"] = order.Service.service_charge.ToString("C");
                            input_ServiceCharge.Attributes["name"] = "price_" + order.service_id;
                            tc4.Controls.AddAt(0, input_ServiceCharge);

                            HtmlGenericControl divDeleteMe = new HtmlGenericControl("div");
                            divDeleteMe.Attributes["class"] = "delete_package";
                            divDeleteMe.InnerHtml = "<img src='images/icon_delete.gif' alt='delete' title='delete'/>";
                            tc4.Controls.AddAt(1, divDeleteMe);

                            HtmlGenericControl div5 = new HtmlGenericControl("div");
                            div5.Attributes["class"] = "floatleft more";
                            div5.InnerHtml += "<a class='getmoreinfo' href='#' id='" + sb.ToString() + "_MoreInfo_" + order.service_id.ToString() + "' title='more info'>more info</a>";
                            tc4.Controls.AddAt(2, div5);

                            tr1.Controls.Add(tc4);
                        }
                        else
                        {
                            tc4.InnerText = "";
                            tr1.Controls.Add(tc4);
                        }
                    }
                    else
                    {
                        HtmlGenericControl input_ServiceCharge = new HtmlGenericControl("input");
                        input_ServiceCharge.Attributes["class"] = "inputSibling";
                        input_ServiceCharge.Attributes["type"] = "hidden";
                        input_ServiceCharge.Attributes["value"] = order.Service.service_charge.ToString("C");
                        input_ServiceCharge.Attributes["name"] = "price_" + order.service_id;
                        tc4.Controls.AddAt(0, input_ServiceCharge);

                        HtmlGenericControl divDeleteMe = new HtmlGenericControl("div");
                        divDeleteMe.Attributes["class"] = "delete_me";
                        divDeleteMe.InnerHtml = "<img src='images/icon_delete.gif' alt='delete' title='delete'/>";
                        tc4.Controls.AddAt(1, divDeleteMe);

                        HtmlGenericControl div5 = new HtmlGenericControl("div");
                        div5.Attributes["class"] = "floatleft more";
                        div5.InnerHtml += "<a class='getmoreinfo' href='#' id='" + sb.ToString() + "_MoreInfo_" + order.service_id.ToString() + "' title='more info'>more info</a>";
                        tc4.Controls.AddAt(2, div5);
                        tr1.Controls.Add(tc4);
                    }
                                        
                    table1.Controls.Add(tr1);
                    divOrderEntry.Controls.Add(table1);

                    j++;

                    var brdMoreInfo = from a in td.GetTable<BoardMoreInfo>()
                                      where a.OrderID == order.order_id && a.ServiceID == order.service_id
                                      select a;

                    if (brdMoreInfo.Count() != 0)
                    {
                        if (brdMoreInfo.Single().InstallType != null)
                        {
                            if (brdMoreInfo.Single().InstallType.Trim().ToUpper() == "ON AWNING" || brdMoreInfo.Single().InstallType.Trim().ToUpper() == "V-SHAPE")
                            {
                                otherprice += 110;
                            }
                        }

                        if (brdMoreInfo.Single().VipService != null)
                        {
                            if (brdMoreInfo.Single().VipService == true)
                            {
                                otherprice += 80;
                            }
                        }
                    }

                    totalPrice = totalPrice + order.Service.service_charge;                    
                }
            }
            divOrdersForPint.InnerHtml += "</table>";

            HtmlGenericControl divTotalPrice = new HtmlGenericControl("div");
            divTotalPrice.Attributes["id"] = "total_price_div";
            divTotalPrice.InnerHtml = "<div class='total'>";
            divTotalPrice.InnerHtml += "<table align='center' cellpadding='0' cellspacing='0' width='100%'>";
            divTotalPrice.InnerHtml += "<tr>";
            divTotalPrice.InnerHtml += "<td width='180' ><strong>Total</strong></td>";
            divTotalPrice.InnerHtml += "<td width='80' class='redtext'><strong>";

            totalPrice = totalPrice + otherprice;
            //totalPrice = totalPrice + otherprice + popupamt;

            divTotalPrice.InnerHtml += "<p id='total_price'>" + "$ " + totalPrice.ToString("0.00") + "</p>";    
            divTotalPrice.InnerHtml += "</strong></td>";
            divTotalPrice.InnerHtml += "</tr>";
            divTotalPrice.InnerHtml += "</table>";
            divTotalPrice.InnerHtml += "</div>";
            
            form.Controls.Add(divTotalPrice);

            divOrdersForPint.InnerHtml += divTotalPrice.InnerHtml;
            divListArea.Controls.Add(hiddenInnerHtml);
            this.divOrderList = divOrdersForPint;
            //"<h3>Floorplan</h3>";

            //"div id='1'"
            //"<input class='input1' type='text' value='Floorplan over 40sq' name='category_1_Floor Plan' ";
            //"<input class='input2' type='text' value='160' name='price_1'> <div class='delete_me'>Delete</div>"
            //"<div id='total_price_div'> <p id='total_price'>$XXX</p>         
        }


        public Predicate<Order> matchCategoryName(Service_Category category)
        {
            return delegate(Order item)
            {
                foreach (Service service in category.MyServices)
                {
                    if (service.service_id == item.service_id)
                    {                        
                        item.Service.service_name = service.service_name;
                        item.Service.service_charge = service.service_charge;
                        return true;
                    }
                }
                return false;
            };
        }

        /*
         * 
   <form id="form2" action="print.aspx" method="post">    
   <div id="ListArea">
   <div><input type="submit" value="Submit" id="submitShoppingList" /><div>
    </div>
    <div id='total_price_div'>
        <p id="total_price">
            $0</p>
    </div>
    </form>
         */
    }
}
